package com.mxx.common.generator.db;

import com.mxx.common.generator.Assert;
import com.mxx.common.generator.GlobalNameConfig;
import com.mxx.common.generator.convert.*;

public class DBFileGeneratorManagerBuilder {

    private DBFileGeneratorManager generatorManager;

    private TypeConvertHandler typeConvertHandler = new TypeConvertHandler();

    private GlobalNameConfig globalNameConfig;

    private TableToEntityInfo tableToEntityInfo;

    private NameConverter nameConverter;

    private DBFileGeneratorManagerBuilder() {

    }

    public static DBFileGeneratorManagerBuilder builder(String url, String username, String password) {
        DBFileGeneratorManagerBuilder managerBuilder = new DBFileGeneratorManagerBuilder();
        managerBuilder.generatorManager = new DBFileGeneratorManager(url, username, password);
        return managerBuilder;
    }

    public DBFileGeneratorManagerBuilder registerTypeConvert(TypeConvert typeConvert) {
        typeConvertHandler.register(typeConvert);
        return this;
    }

    public DBFileGeneratorManagerBuilder setGlobalNameConfig(GlobalNameConfig globalNameConfig) {
        this.globalNameConfig = globalNameConfig;
        return this;
    }

    public DBFileGeneratorManagerBuilder setNameConverter(NameConverter nameConverter) {
        this.nameConverter = nameConverter;
        return this;
    }

    public DBFileGeneratorManager complete() {
        Assert.notFalse(globalNameConfig == null, "globalNameConfig is null");
        NameConverter nameConverter = this.nameConverter == null ? new HumpNameConverter(globalNameConfig) : this.nameConverter;
        TableToEntityInfo tableToEntityInfo = this.tableToEntityInfo == null ? new DefaultTableToEntityInfo(globalNameConfig, nameConverter, typeConvertHandler) : this.tableToEntityInfo;


        generatorManager.setNameConverter(nameConverter);
        generatorManager.setTableToEntityInfo(tableToEntityInfo);
        generatorManager.setGlobalNameConfig(globalNameConfig);
        generatorManager.setTypeConvertHandler(typeConvertHandler);
        return generatorManager;
    }
}